package com.movie.controller;

import com.movie.beans.SysUser;
import com.movie.beans.base.AjaxResult;
import com.movie.utils.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
@Controller
public class LoginController {

    @GetMapping(value = "/login")
    public String login(){
        return "/login";
    };

    @GetMapping(value = "/clogin")
    public String clogin(){
        return "/clogin";
    };

    @PostMapping(value = "/login")
    @ResponseBody
    public AjaxResult ajaxLogin(String loginName, String password){

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(loginName,password);
        try {
            subject.login(token);
            return AjaxResult.success();
        } catch (UnknownAccountException e)
        {
            return AjaxResult.error(e.getMessage());
        }catch (IncorrectCredentialsException e){
            return AjaxResult.error(e.getMessage());
        }catch (DisabledAccountException e){
            return AjaxResult.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/unauth")
    public String unAuth(){
        return "/unauth";
    }

    @GetMapping("/isLogin")
    @ResponseBody
    public boolean isLogin(){
        SysUser user = ShiroUtil.getSysUser();
        if (user==null){
            return false;
        }
        return true;
    }


}
